﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Audio;

namespace ZombieMonkeys.Vibria.Audio
{

    public class SoundLibrary
    {
        private static SoundLibrary instance;

        private Dictionary<string, SoundEffect> soundEffects;

        private int soundEffectCount;

        /// <summary>
        /// Get's the number of sound effects currently in the Library
        /// </summary>
        public int SoundEffectCount
        {
            get { return soundEffects.Count; }
            
        }
        /// <summary>
        /// private constructor initializes the SoundEffect Dictionary
        /// </summary>
        private SoundLibrary()
        {
            soundEffects = new Dictionary<string, SoundEffect>();
        }

        /// <summary>
        /// Get the one and only SoundLibrary object instance
        /// </summary>
        /// <returns>the single instance of the SoundLibrary</returns>
        public static SoundLibrary GetSoundLibrary()
        { 
            if (instance == null)
            {
                instance = new SoundLibrary();
            }
            return instance;
        }

        //you should house all of the names for the SoundEffects in the GUI, so that you can both access 
        //the dictionary later, and also add them with values that you will know they will have as a key
        /// <summary>
        /// Add a SoundEffect to the SoundLibary
        /// </summary>
        /// <param name="newSoundEffect"></param>
        /// <param name="soundName"></param>
        public void AddSoundEffect(SoundEffect newSoundEffect, string soundName)
        {
            bool exists = false;

            exists = verifyNotInDictionary(soundName);

            if (!exists)
            {
                soundEffects.Add(soundName, newSoundEffect);
            }
        }

        public SoundEffect GetSoundEffectByKey(string key)
        { 
            SoundEffect requestedSoundEffect;

            if (verifyInDictionary(key))
            {
                requestedSoundEffect = soundEffects[key];
            }
            else
            { 
                //if it's not found then set an object and return it
                requestedSoundEffect = new SoundEffect(new byte[1], 100, AudioChannels.Stereo);
            }
            return requestedSoundEffect;
        }

        private bool verifyInDictionary(string soundName)
        {
            bool exists = false;
            if (soundEffects.ContainsKey(soundName))
            {
                exists = true;
            }
            return exists;
        }


        private bool verifyNotInDictionary(string soundName)
        {
            bool notInDictionary = false ;
            if (!soundEffects.ContainsKey(soundName))
            {
                notInDictionary = true ;
            }
            return notInDictionary;
        }
    }
}
